package top.frsf.manage.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.transaction.annotation.Transactional;
import top.frsf.common.core.redis.RedisCache;
import top.frsf.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.frsf.common.utils.uuid.IdUtils;
import top.frsf.common.utils.uuid.UUID;
import top.frsf.manage.domain.Channel;
import top.frsf.manage.domain.Node;
import top.frsf.manage.domain.VmType;
import top.frsf.manage.domain.dto.DeviceDataDTO;
import top.frsf.manage.mapper.ChannelMapper;
import top.frsf.manage.mapper.NodeMapper;
import top.frsf.manage.mapper.VendingMachineMapper;
import top.frsf.manage.domain.VendingMachine;
import top.frsf.manage.mapper.VmTypeMapper;
import top.frsf.manage.service.IChannelService;
import top.frsf.manage.service.IVendingMachineService;

import javax.annotation.Resource;

/**
 * 设备设备Service业务层处理
 *
 * @author ruoyi
 * @date 2024-11-14
 */
@Service
public class VendingMachineServiceImpl implements IVendingMachineService {
    @Autowired
    private VendingMachineMapper vendingMachineMapper;

    @Resource
    private VmTypeMapper vmTypeMapper;

    @Resource
    private NodeMapper nodeMapper;

    @Resource
    private ChannelMapper channelMapper;

    @Autowired
    private IChannelService channelService;
    @Autowired
    private RedisCache redisCache;
    /**
     * 查询设备设备
     *
     * @param id 设备设备主键
     * @return 设备设备
     */
    @Override
    public VendingMachine selectVendingMachineById(Long id) {
        return vendingMachineMapper.selectVendingMachineById(id);
    }

    /**
     * 查询设备设备列表
     *
     * @param vendingMachine 设备设备
     * @return 设备设备
     */
    @Override
    public List<VendingMachine> selectVendingMachineList(VendingMachine vendingMachine) {
        return vendingMachineMapper.selectVendingMachineList(vendingMachine);
    }

    /**
     * 新增设备设备
     *
     * @param vendingMachine 设备设备
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertVendingMachine(VendingMachine vendingMachine) {
        /* 新增设备 */
        // 生成设备唯一编号
        vendingMachine.setInnerCode(IdUtils.simpleUUID());
        // 查询设备类型表，设置设备容量
        VmType vmType = vmTypeMapper.selectVmTypeById(vendingMachine.getVmTypeId());
        vendingMachine.setChannelMaxCapacity(vmType.getChannelMaxCapacity());
        // 查询点位表，设置点位信息
        Node node = nodeMapper.selectNodeById(vendingMachine.getNodeId());
        vendingMachine.setAddr(node.getAddress());
        vendingMachine.setBusinessType(node.getBusinessType());
        vendingMachine.setRegionId(node.getRegionId());
        vendingMachine.setPartnerId(node.getPartnerId());
        // 设备状态 0-未投放
        vendingMachine.setVmStatus(0L);
        vendingMachine.setCreateTime(DateUtils.getNowDate());
        int result = vendingMachineMapper.insertVendingMachine(vendingMachine);

        /* 新增货道 */
        // 货道行列编号 1-1 1-2 2-1 ...
        ArrayList<Channel> channelList = new ArrayList<>();
        for (int i = 0; i < vmType.getVmRow(); i++) {
            for (int j = 0; j < vmType.getVmCol(); j++) {
                // 封装货道对象
                Channel channel = new Channel();
                channel.setChannelCode(i + "-" + j);
                channel.setVmId(vendingMachine.getId());
                channel.setInnerCode(vendingMachine.getInnerCode());
                channel.setMaxCapacity(vmType.getChannelMaxCapacity());
                channel.setCreateTime(DateUtils.getNowDate());
                channel.setUpdateTime(DateUtils.getNowDate());
                channelList.add(channel);
            }
        }
        channelService.batchInsertChannel(channelList);
        return result;
    }

    /**
     * 修改设备设备
     *
     * @param vendingMachine 设备设备
     * @return 结果
     */
    @Override
    public int updateVendingMachine(VendingMachine vendingMachine) {
        // 查询点位表，同步冗余字段
        if(vendingMachine.getNodeId() != null) {
            Node node = nodeMapper.selectNodeById(vendingMachine.getNodeId());
            vendingMachine.setAddr(node.getAddress());
            vendingMachine.setBusinessType(node.getBusinessType());
            vendingMachine.setRegionId(node.getRegionId());
            vendingMachine.setPartnerId(node.getPartnerId());
        }

        vendingMachine.setUpdateTime(DateUtils.getNowDate());
        return vendingMachineMapper.updateVendingMachine(vendingMachine);
    }

    /**
     * 批量删除设备设备
     *
     * @param ids 需要删除的设备设备主键
     * @return 结果
     */
    @Override
    public int deleteVendingMachineByIds(Long[] ids) {
        return vendingMachineMapper.deleteVendingMachineByIds(ids);
    }

    /**
     * 删除设备设备信息
     *
     * @param id 设备设备主键
     * @return 结果
     */
    @Override
    public int deleteVendingMachineById(Long id) {
        return vendingMachineMapper.deleteVendingMachineById(id);
    }

    @Override
    public VendingMachine selectVendingMachineByInnerCode(String innerCode) {
        return vendingMachineMapper.selectVendingMachineByInnerCode(innerCode);
    }

    /**
     * 发送设备数据
     * @param deviceData
     * @return   发送是否正常
     */
    public  boolean sendDeviceData(DeviceDataDTO deviceData){
        String dataKey="dataKey_"+deviceData.getDeviceCode()+"_"+deviceData.getDataType();
        redisCache.setCacheObject(dataKey,deviceData.getData());
        return true;
    }

    /**
     * 获取设备数据
     * @param deviceCode
     * @param dataType
     * @return  设备数据
     */
    public  String getDeviceData(String  deviceCode, String dataType){
        String dataKey="dataKey_"+deviceCode+"_"+dataType;
        return redisCache.getCacheObject(dataKey);
    }
}
